# -*- coding: utf-8 -*-
import scrapy
from scrapy.http import Request
from urllib import parse

from DangDangSpider.items import DangdangArticleItem


class DangdangSpider(scrapy.Spider):
    name = 'dangdang'
    allowed_domains = ['search.dangdang.com', 'product.dangdang.com']

    # start_urls = ['http://search.dangdang.com/?key=%B4%E5%C9%CF%B4%BA%CA%F7/']
    start_urls = ['http://search.dangdang.com/?key=%B4%F3%CA%FD%BE%DD/']


    def parse(self, response):
        # p25228608 > p.detail
        post_nodes = response.css("#component_0__0__6612 li")
        for post_node in post_nodes:
            image_url = post_node.css(".pic img::attr(data-original)").extract_first("")
            post_url = post_node.css(".pic::attr(href)").extract_first("")
            information=post_node.css("p.detail::text").extract_first("")
            yield Request(url=parse.urljoin(response.url, post_url), meta={"front_image_url": image_url,"information":information},
                          callback=self.parse_detail)
        next_urls = response.css(".paging .next a::attr(href)").extract_first("")
        if next_urls:
            yield Request(url=parse.urljoin(response.url, next_urls), callback=self.parse)
        else:
            print('end')



    def parse_detail(self,response):
        article_item = DangdangArticleItem()
        article_item["more_book_name"]=response.css("#product_info .name_info h1::attr(title)").extract_first()
        article_item["more_book_author"]=response.css("#author a::text").extract_first()
        article_item["more_book_publish"]=response.css("#product_info > div.messbox_info > span:nth-child(2) > a::text").extract_first()
        article_item["more_book_img"]=response.meta.get("front_image_url", "")
        more_book_realprice=response.css("#dd-price::text").extract()[1].strip().split(".")
        article_item["more_book_price"]=more_book_realprice[0]
        article_item["more_book_littleprice"]=more_book_realprice[1]
        article_item["more_book_kind"]=response.css("#breadcrumb > a:nth-child(3)::text").extract_first()
        article_item["more_book_put_time"]=response.css("#product_info > div.messbox_info > span:nth-child(3)::text").extract_first().strip().replace("出版时间:", "")
        article_item["more_book_information"]=response.meta.get("information", "")
        article_item["more_book_oldprice"]=response.css("#original-price::text").extract()[1].strip()

        yield article_item